import json
from zwz.mjOverseas.api.get_log import get_log
from zwz.mjOverseas.api.read_info import ReadYaml
from zwz.mjOverseas.api.run_method import RunMethod


class AllApi(object):
    def __init__(self):
        self.run = RunMethod()
        self.read = ReadYaml()
        self.logger = get_log()

    # 接口请求封装
    def send_request(self, api_name):
        try:
            # 获取接口请求参数
            method = self.read.get_method(api_name)
            url = self.read.get_url(api_name)
            headers = self.read.get_headers(api_name)
            data = self.read.get_data(api_name)
            # 区分Get和Post方法
            if method == "Get":
                response = self.run.run_main(method, url, headers, data)
            elif method == "Post":
                response = self.run.run_main(method, url, headers, data)
            return response
        except Exception as e:
            self.logger.info("接口访问出错啦~ %s" % e)
            print(e)
    # 获取预期结果，方便断言时直接使用
    def get_expect(self, api_name):
        try:
            # 获取配置文件中的预期结果
            expect = self.read.get_expected(api_name)
            # print(expect)
            return expect
        except Exception as e:
            self.logger.info("获取预期结果出错啦~ %s" % e)
            print(e)

if __name__ == "__main__":
    all_api = AllApi()
    print((all_api.send_request("index")))
